﻿using System;
using System.Windows.Forms;
using log4net;

namespace BarberShop
{
    public partial class MainForm : Form
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof (MainForm));

        public MainForm()
        {
            InitializeComponent();

            servicesBindingSource.DataSource = dbDataSet.Services;

            providedServisesComboBox.DataSource = servicesBindingSource;
            providedServisesComboBox.DisplayMember = dbDataSet.Services.NameColumn.ColumnName;
            providedServisesComboBox.ValueMember = dbDataSet.Services.ServiceIdColumn.ColumnName;

            SetDefaultInfoPanel();

            Log.Info("MainForm initialized");
        }

        private void FormLoad(object sender, EventArgs e)
        {
            LoadDataFromDb();
            TryToSelectFistClient();
            UpdateDbStatisticsLable();

            removeClientButton.Enabled = clientsDataGridView.Rows.Count > 0;
            addOperationButton.Enabled = GetSelectedClientId() != 0;
        }

        private void AddClientButtonClick(object sender, EventArgs e)
        {
            AddClient();
        }

        private void AddServiceButtonClick(object sender, EventArgs e)
        {
            AddService();
        }

        private void AddOperationButtonClick(object sender, EventArgs e)
        {
            AddOperation();
        }

        private void EditClientButtonClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            EditClient(GetSelectedClientId());
        }

        private void RemoveClientButtonClick(object sender, EventArgs e)
        {
            DeleteClient(GetSelectedClientId());
        }

        private void RemoveOperationButtonClick(object sender, EventArgs e)
        {
            DeleteOperation(GetSelectedOperationId());
        }

        private void ClientsDataGridViewCellClick(object sender, DataGridViewCellEventArgs e)
        {
            UpdateClietInfoPanel(GetSelectedClientId());
        }

        private void AboutToolStripMenuItemClick(object sender, EventArgs e)
        {
            new AboutForm().ShowDialog();
        }

        private void FilterButtonClick(object sender, EventArgs e)
        {
            UpdateFilter();
        }

        private void ClearFilterLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ClearFilter();
        }

        private void EnableOperationTypeFilterCheckBoxCheckStateChanged(object sender, EventArgs e)
        {
            providedServisesComboBox.Enabled = enableOperationTypeFilterCheckBox.Checked;
            providedServicesLable.Enabled = enableOperationTypeFilterCheckBox.Checked;
        }

        private void ServicesManagerToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenServicesManager();
        }

        private void FilterPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 13: //Enter key
                    UpdateFilter();
                    break;
                case 27: //Esc key
                    ClearFilter();
                    break;
            }
        }

    }
}